#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
using namespace std;

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        vector<int> hash(fruits.size(), 0);
        int ret = 0;
        for (int left = 0, right = 0, kind = 0; right < fruits.size(); ++right)
        {
            if (hash[fruits[right]]++ == 0) ++kind;
            while (kind > 2)
            {
                if (hash[fruits[left++]]-- == 1) --kind;
            }
            if (kind <= 2) ret = max(ret, right - left + 1);
        }
        return ret;
    }
};